using System;

namespace SonarCaptain
{
    class Game
    {
        #region static resources
        public static int ROW_COUNT = 10;
        public static int COLUMN_COUNT = 10;
        public static int ISLAND_COUNT = 8;
        #endregion

        #region properties
        public string RoomName {get; private set;}
        public Routine MyRoutine {get;} = new Routine();
        public Map WarFieldMap {get;} = new Map();
        #endregion

        #region start
        private bool SetRoomName(string roomName)
        {
            if (roomName.Length == 6)
            {
                RoomName = roomName;
                return true;
            }
            else
            {
                return false;
            }
        }
        public void StartHandler()
        {
            while (true)
            {
                Console.WriteLine("请输入6位房间号：");
                string roomName = Console.ReadLine();
                if (SetRoomName(roomName))
                {
                    break;
                }
                Console.Write("房间号不合法，");
            }
        }
        #endregion

        #region initial
        private (int, int) GetPosition(string position)
        {
            int rowPosition = -1;
            int colPosition = -1;
            if (position.Length < 2 || position.Length > 3)
            {
                return (-1, -1);
            }
            if (position[0]>='a' && position[0]<='z')
            {
                rowPosition = position[0] - 'a';
            }
            else if (position[0]>='A' && position[0]<='Z')
            {
                rowPosition = position[0] - 'A';
            }
            if (position[1]>='0' && position[1]<='9')
            {
                colPosition = position[1] - '0';
            }
            if (position.Length == 3)
            {
                colPosition *= 10;
                if (position[2]>='0' && position[2]<='9')
                {
                    colPosition += position[2] - '0';
                }
                else
                {
                    colPosition = -1;
                }
            }
            return (rowPosition, colPosition);
        }
        private bool ValidatePosition((int, int) position)
        {
            return position.Item1 >= 0 && position.Item1 <= 25 && position.Item2 >= 0 && position.Item2 <= 25;
        }
        public void InitialHandler()
        {
            while (true)
            {
                Console.Clear();
                WarFieldMap.PrintMap();
                Console.WriteLine("请输入初始化位置（如A3、J07、D22等）：");
                var position = GetPosition(Console.ReadLine());
                if (ValidatePosition(position))
                {
                    MyRoutine.StartPosition = position;
                    break;
                }
                Console.Write("位置不合法，");
            }
        }
        #endregion
    }
}